import {
  createResponseComposition,
  context,
  rest,
  ResponseResolver,
  setupWorker,
  response,
  RestRequest,
} from 'msw';
import db from '~/DB';
import { getToken, getTokenInfo } from '../utils/token';
import { Token } from '~~/DB/types/token.d';
import { Status } from '~~/DB/types/status.d';
import { hasMenu } from '../utils/menu';
import { validateToken } from './token';

type T = Parameters<typeof rest['all']>[1];

const a: T = async (req, res, ctx) => {};
// interface Req extends T {}

// export async function validateMenu(req: Req, key: string, callback: () => any) {
//   // const a = await hasMenu(req, 'menu');
//   return validateToken(req, async () => {
//     const isValid = await hasMenu(req, key);
//     if (!isValid) {
//       return response(
//         context.status(403),
//         context.json({ message: '无权访问' }),
//       );
//     }

//     return callback();
//   });
// }

export const validateMenu = (key: string, resolver: T): T =>
  validateToken(async (req, res, ctx) => {
    const isValid = await hasMenu(req, key);
    if (!isValid) {
      return res(ctx.status(403), context.json({ message: '无权访问' }));
    }
    return resolver(req, res, ctx);
  });
